/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'page_share_setting',
                condition: {},
                model: {
                    url:_global.url.api + 'page_share_setting',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove',
                    },
                    edit: function(e) {
                        _this.data = this.data;
                        var model = this;
                        e.preventDefault();
                        dialog({
                            'title':'修改页面',
                            'content': template('add-tpl',this.data),
                            'width':600,
                            ok: function() {
                                _this.data.name = $("input[name='name']").val();
                                if(_this.data.name==''||_this.data.name==undefined){
                                    Alert({msg:'请填写页面名称',type:'error'});
                                    return false;
                                }
                                _this.data.page_url = $("input[name='page_url']").val();
                                if(_this.data.page_url==''||_this.data.page_url==undefined){
                                    Alert({msg:'请填写页面地址',type:'error'});
                                    return false;
                                }
                                _this.data.url = $("input[name='url']").val();
                                if(_this.data.url==''||_this.data.url==undefined){
                                    Alert({msg:'请填写分享地址',type:'error'});
                                    return false;
                                }
                                _this.data.title = $("input[name='title']").val();
                                _this.data.desc = $("textarea[name='desc']").val();
                                _this.data.img = $("input[name='imgs']").val();
                                _this.data.id = _this.data.id;
                                if(model.put()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.fetch();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('.btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title:'添加页面',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function () {
                        _this.data.name = $("input[name='name']").val();
                        if(_this.data.name==''||_this.data.name==undefined){
                            Alert({msg:'请填写页面名称',type:'error'});
                            return false;
                        }
                        _this.data.page_url = $("input[name='page_url']").val();
                        if(_this.data.page_url==''||_this.data.page_url==undefined){
                            Alert({msg:'请填写页面地址',type:'error'});
                            return false;
                        }
                        _this.data.url = $("input[name='url']").val();
                        if(_this.data.url==''||_this.data.url==undefined){
                            Alert({msg:'请填写分享地址',type:'error'});
                            return false;
                        }
                        _this.data.title = $("input[name='title']").val();
                        _this.data.desc = $("textarea[name='desc']").val();
                        _this.data.img = $("input[name='imgs']").val();
                        if(new Model({
                                url: _global.url.api + 'page_share_setting',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    },cancel: function () {}
                });
                _this.dialog.show();
            });

            //上传图片
            $('body').on('click', '#btn-upload', function(e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function() {
                        console.log('contentWindow',$("#upload-frame")[0].contentWindow.image);
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function() {}
                });
                _this.upload_dialog.show();
            });
        },
        imgPreview: function(img) {
            console.log('this.data',this.data);
            if(typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }
    };

    new index();

});